﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using SIGERP_Entidades;
using System.Data;

namespace SIGERP_Datos
{
    public class HndArea
    {

        public static List<Area> getAll()
        {
            DataTable tabla = Conexion.ejecutaSelect("SELECT * FROM dbo.area" + " ORDER BY descripcion");
            var areas = new List<Area>();
            foreach (DataRow dtRow in tabla.Rows)
            {
                Area a = new Area(dtRow);
                areas.Add(a);
            }
            return areas;
        }

        public static Area getOne(int id)
        {
            DataTable tabla = Conexion.ejecutaSelect("SELECT * FROM dbo.area WHERE id_area=" + id+" ORDER BY descripcion");
            DataRow dtRow = tabla.Rows[0];
            Area a = new Area(dtRow);
            return a;
        }

        public static Boolean insert(Area area)
        {
            SqlCommand comando = new SqlCommand();
            comando.CommandText = "INSERT INTO dbo.area (descripcion,habilitado) VALUES (@descripcion,1)";
            comando.Parameters.AddWithValue("@descripcion", area.descripcion);
            return Conexion.ejecutaComando(comando);
        }

        public static Boolean update(Area area)
        {
            SqlCommand comando = new SqlCommand();
            comando.CommandText = "UPDATE dbo.area SET descripcion=@descripcion,habilitado=@habilitado WHERE id_area=@id";
            comando.Parameters.AddWithValue("@descripcion", area.descripcion);
            if (area.habilitado) comando.Parameters.AddWithValue("@habilitado", 1);
            else comando.Parameters.AddWithValue("@habilitado", 0);
            comando.Parameters.AddWithValue("@id", area.id);
          
            return Conexion.ejecutaComando(comando);
        }

        public static Boolean delete(Area area)
        {
            SqlCommand comando = new SqlCommand();
            comando.CommandText = "DELETE FROM dbo.area WHERE id_area=@id";
            comando.Parameters.AddWithValue("@id", area.id);
            return Conexion.ejecutaComando(comando);
        }

        public static List<Area> getAllEnabled()
        {
            DataTable tabla = Conexion.ejecutaSelect("SELECT * FROM dbo.area WHERE habilitado=1 " + " ORDER BY descripcion");
            var areas = new List<Area>();
            foreach (DataRow dtRow in tabla.Rows)
            {
                Area a = new Area(dtRow);
                areas.Add(a);
            }
            return areas;
        }

        
    }
}
